<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('库存物品列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>物品名称：</label>
                            <input type="text" name="name" id="name"/>
                        </li>
                        <li>
                            <label>购买账户：</label>
                            <select name="account" id="account">
                                <option value="">所有</option>
                                <option value="1">账号1</option>
                                <option value="2">账号2</option>
                                <option value="3">账号3</option>
                                <option value="4">账号4</option>
                                <option value="5">账号5</option>
                                <option value="6">账号6</option>
                            </select>
                        </li>
                        <!--                            <li>-->
                        <!--                                <label>单个成本均价：</label>-->
                        <!--                                <input type="text" name="unitCost"/>-->
                        <!--                            </li>-->
                        <!--                            <li>-->
                        <!--                                <label>库存数量：</label>-->
                        <!--                                <input type="text" name="quantity"/>-->
                        <!--                            </li>-->
                        <!--                            <li>-->
                        <!--                                <label>历史盈利：</label>-->
                        <!--                                <input type="text" name="historyProfit"/>-->
                        <!--                            </li>-->
                        <!--                            <li>-->
                        <!--                                <label>可售数量：</label>-->
                        <!--                                <input type="text" name="availableQuantity"/>-->
                        <!--                            </li>-->
                        <!--                            <li>-->
                        <!--                                <label>steam单价：</label>-->
                        <!--                                <input type="text" name="steamPrice"/>-->
                        <!--                            </li>-->
                        <!--                            <li>-->
                        <!--                                <label>UU单价：</label>-->
                        <!--                                <input type="text" name="uuPrice"/>-->
                        <!--                            </li>-->
                        <!--                            <li>-->
                        <!--                                <label>C5单价：</label>-->
                        <!--                                <input type="text" name="c5Price"/>-->
                        <!--                            </li>-->
                        <!--                            <li>-->
                        <!--                                <label>Buff单价：</label>-->
                        <!--                                <input type="text" name="buffPrice"/>-->
                        <!--                            </li>-->
                        <!--                        <li>-->
                        <!--                            <label>创建时间：</label>-->
                        <!--                            <input type="text" class="time-input" placeholder="请选择创建时间" name="createDate"/>-->
                        <!--                        </li>-->
                        <!--                            <li>-->
                        <!--                                <label>更新时间：</label>-->
                        <!--                                <input type="text" class="time-input" placeholder="请选择更新时间" name="updateDate"/>-->
                        <!--                            </li>-->
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="col-sm-12 search-collapse">
            <div class="col-sm-12 select-table table-striped">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">库存汇总</h3>
                    </div>
                    <table id="total-table" class="table table-bordered">
                        <thead>
                        <tr>
                            <th>总库存数量</th>
                            <th>总可售数量</th>
                            <th>总成本价值</th>
                            <th>总库存价值</th>
                            <th>总历史盈利</th>
                            <th>UU预估总盈利</th>
                            <th>C5预估总盈利</th>
                            <th>Buff预估总盈利</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td id="total-quantity">0</td>
                            <td id="total-available">0</td>
                            <td id="total-cost">¥0.00</td>
                            <td id="total-inventory-price">¥0.00</td>
                            <td id="total-history-profit">¥0.00</td>
                            <td id="total-uu-profit">¥0.00</td>
                            <td id="total-c5-profit">¥0.00</td>
                            <td id="total-buff-profit">¥0.00</td>
                        </tr>
                        </tbody>
                    </table>
                </div>

            </div>
        </div>
        <div class="btn-group-sm" id="toolbar" role="group">
            <!--                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:inventory:add">-->
            <!--                    <i class="fa fa-plus"></i> 添加-->
            <!--                </a>-->
            <a class="btn btn-primary single disabled" onclick="$.operate.edit()"
            >
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
            >
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    // var editFlag = [[${@permission.hasPermi('system:inventory:edit')}]];
    // var removeFlag = [[${@permission.hasPermi('system:inventory:remove')}]];
    var prefix = ctx + "system/inventory";
    var totalPrefix = ctx + "system/inventory/inventoryTotal"; // 汇总数据接口前缀
    // 加载汇总数据
    function loadTotalData() {
        var nameValue = $('#name').val();
        $.ajax({
            url: totalPrefix,
            type: 'post',
            dataType: 'json',
            data: {
                name: nameValue.split('').join('%'),
                account: $('#account').val()
            },
            pageSize: 20,
            beforeSend: function () {
                // 显示加载状态
                $('#total-table tbody td').html('<i class="fa fa-spinner fa-spin"></i> 加载中...');
            },
            success: function (result) {
                console.log(result)
                if (result.code === 0) {
                    console.log(result.data)
                    updateTotalTable(result.data);
                } else {
                    $.modal.alertError("汇总数据加载失败：" + result.msg);
                    $('#total-table tbody td').text('-');
                }
            },
            error: function () {
                $.modal.alertError("汇总数据加载失败");
                $('#total-table tbody td').text('-');
            }
        });
    }

    // 更新汇总表格
    function updateTotalTable(totals) {
        if (!totals) return;
        $('#total-quantity').text(totals.totalQuantity || 0);
        $('#total-available').text(totals.totalAvailableQuantity || 0);
        $('#total-cost').text('¥' + ((totals.totalCost || 0) / 100).toFixed(2));
        $('#total-inventory-price').text('¥' + ((totals.totalInventoryPrice || 0) / 100).toFixed(2));
        $('#total-history-profit').text('¥' + ((totals.totalHistoryProfit || 0) / 100).toFixed(2));
        $('#total-uu-profit').text('¥' + ((totals.totalUuProfit || 0) / 100).toFixed(2));
        $('#total-c5-profit').text('¥' + ((totals.totalC5Profit || 0) / 100).toFixed(2));
        $('#total-buff-profit').text('¥' + ((totals.totalBuffProfit || 0) / 100).toFixed(2));

        // 添加颜色标识
        colorizeProfitCells();
    }

    // 为盈利单元格添加颜色标识
    function colorizeProfitCells() {
        var profitCells = ['#total-history-profit', '#total-uu-profit', '#total-c5-profit', '#total-buff-profit'];

        profitCells.forEach(function (cellId) {
            var cell = $(cellId);
            var value = parseFloat(cell.text().replace('¥', ''));

            cell.removeClass('text-success text-danger text-muted');

            if (value > 0) {
                cell.addClass('text-success');
            } else if (value < 0) {
                cell.addClass('text-danger');
            } else {
                cell.addClass('text-muted');
            }
        });
    }

    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "库存物品",
            // 表格操作后刷新汇总数据
            onLoadSuccess: function (data) {
                loadTotalData();
            },
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '${comment}',
                    visible: false
                },
                {
                    field: 'account',
                    title: '购买账户',
                    sortable: true,
                    formatter: function (value, row, index) {
                        switch (value) {
                            case "1":
                                return '账户1';
                            case "2":
                                return '账户2';
                            case "3":
                                return '账户3';
                            case "4":
                                return '账户4';
                            case "5":
                                return '账户5';
                            case "6":
                                return '账户6';
                            default:
                                return '未知账户';
                        }
                    }
                },
                {
                    field: 'name',
                    title: '物品名称',
                    sortable: true
                },
                {
                    field: 'marketHashName',
                    title: '市场散列名称',
                    visible: false
                },
                {
                    field: 'unitCost',
                    title: '成本均价',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return (row.unitCost / 100).toFixed(2);

                    }
                },
                {
                    field: 'quantity',
                    title: '库存数量',
                    sortable: true
                },
                {
                    field: 'unitCostTotal',
                    title: '成本总价',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return (row.unitCostTotal / 100).toFixed(2);
                    }
                },
                {
                    field: 'historyProfit',
                    title: '历史盈利',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return (row.historyProfit / 100).toFixed(2);
                    }
                },
                {
                    field: 'availableQuantity',
                    title: '可售数量',
                    sortable: true
                },
                {
                    field: 'steamPrice',
                    title: 'steam单价',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return (row.steamPrice / 100).toFixed(2);
                    }
                },
                {
                    field: 'steamBalancePrice',
                    title: 'steam余额价格',
                    sortable: true,
                    formatter: function (value, row, index) {
                        var displayValue = (row.steamBalancePrice / 100).toFixed(2);
                        if (row.steamBalancePrice < row.uuPrice) {
                            return '<span style="color: red; font-weight: bold;">' + displayValue + '</span>';
                        } else if (row.steamBalancePrice < row.c5Price) {
                            return '<span style="color: orange; font-weight: bold;">' + displayValue + '</span>';
                        } else if (row.steamBalancePrice < row.buffPrice) {
                            return '<span style="color: green; font-weight: bold;">' + displayValue + '</span>';
                        } else {
                            return displayValue;
                        }
                    }
                },
                {
                    field: 'steamBalanceDiscount',
                    title: 'steam余额折扣',
                    sortable: true,
                    formatter: function (value, row, index) {
                        let percentage = row.steamBalanceDiscount / 100;
                        let displayValue;
                        if (row.steamBalanceDiscount !== 0) {
                            displayValue = percentage.toFixed(2) + '%';
                        } else {
                            displayValue = '0%';
                        }
                        // 如果百分比在70%以内，添加红色样式
                        if (percentage <= 70 && percentage > 0) {
                            return '<span style="color: red; font-weight: bold;">' + displayValue + '</span>';
                        }
                        return displayValue;
                    }
                },
                {
                    field: 'steamBalanceDiscountByUu',
                    title: 'UU余额折扣',
                    sortable: true,
                    formatter: function (value, row, index) {
                        let percentage = row.steamBalanceDiscountByUu / 100;
                        let displayValue;
                        if (row.steamBalanceDiscountByUu !== 0) {
                            displayValue = percentage.toFixed(2) + '%';
                        } else {
                            displayValue = '0%';
                        }
                        // 如果百分比在70%以内，添加红色样式
                        if (percentage <= 70 && percentage > 0) {
                            return '<span style="color: red; font-weight: bold;">' + displayValue + '</span>';
                        }
                        return displayValue;
                    }
                },
                {
                    field: 'uuPrice',
                    title: 'UU单价',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return row.uuPrice / 100;
                    }
                },
                {
                    field: 'uuProfit',
                    title: 'UU盈利',
                    sortable: true,
                    formatter: function (value, row, index) {
                        if (row.unitCost !== 0 && row.uuPrice !== 0) {
                            return ((row.uuPrice * 0.99 - row.unitCost) / 100).toFixed(2);
                        } else {
                            return 0;
                        }
                    }
                },
                {
                    field: 'uuTotalProfit',
                    title: 'UU总盈利',
                    sortable: true,
                    formatter: function (value, row, index) {
                        if (row.unitCost !== 0 && row.uuPrice !== 0) {
                            return ((row.uuPrice * 0.99 - row.unitCost) / 100 * row.quantity).toFixed(2);
                        } else {
                            return 0;
                        }
                    }
                },
                {
                    field: 'c5Price',
                    title: 'C5单价',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return row.c5Price / 100;
                    }
                },
                {
                    field: 'c5Profit',
                    title: 'C5盈利',
                    sortable: true,
                    formatter: function (value, row, index) {
                        if (row.unitCost !== 0 && row.c5Price !== 0) {
                            return ((row.c5Price - row.unitCost) / 100).toFixed(2);
                        } else {
                            return 0;
                        }
                    }
                },
                {
                    field: 'c5TotalProfit',
                    title: 'C5总盈利',
                    sortable: true,
                    formatter: function (value, row, index) {
                        if (row.unitCost !== 0 && row.c5Price !== 0) {
                            return ((row.c5Price - row.unitCost) / 100 * row.quantity).toFixed(2);
                        } else {
                            return 0;
                        }
                    }
                },
                {
                    field: 'buffPrice',
                    title: 'Buff单价',
                    sortable: true,
                    formatter: function (value, row, index) {
                        return row.buffPrice / 100;
                    }
                },
                {
                    field: 'buffProfit',
                    title: 'Buff盈利',
                    sortable: true,
                    formatter: function (value, row, index) {
                        if (row.unitCost !== 0 && row.buffPrice !== 0) {
                            return ((row.buffPrice * 0.975 - row.unitCost) / 100).toFixed(2);
                        } else {
                            return 0;
                        }
                    }
                },
                {
                    field: 'buffTotalProfit',
                    title: 'Buff总盈利',
                    sortable: true,
                    formatter: function (value, row, index) {
                        if (row.unitCost !== 0 && row.buffPrice !== 0) {
                            return ((row.buffPrice * 0.975 - row.unitCost) / 100 * row.quantity).toFixed(2);
                        } else {
                            return 0;
                        }
                    }
                },
                {
                    field: 'createDate',
                    title: '创建时间',
                    sortable: true,
                    isAsc: 'desc'
                },
                {
                    field: 'updateDate',
                    title: '更新时间',
                    sortable: true,
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });
</script>
</body>
</html>